package controller;

import java.sql.SQLException;
import java.util.ArrayList;

import db.DBConnection;
import db.DBCustomer;
import db.DBPerson;
import db.IFCustomer;
import db.IFPerson;
import model.City;
import model.Customer;

public class CustomerController 
{
	private static CustomerController instance = null;

	public static CustomerController getInstance()
	{
		if(instance == null)
			instance = new CustomerController();

		return instance;
	}

	public CustomerController()
	{

	}

	public int createCustomer(String username, String password, boolean active, City city, String email, String phoneNo)
	{
		int result = -3;

		try 
		{
			Customer newCustomer = new Customer();
			newCustomer.setUsername(username);
			newCustomer.setPassword(password);
			newCustomer.setActive(active);
			newCustomer.setCity(city);
			newCustomer.setEmail(email);
			newCustomer.setPhoneNo(phoneNo);

			DBConnection.startTransaction();
			IFCustomer dbCustomer = new DBCustomer();			
			result = dbCustomer.createCustomer(newCustomer);
			DBConnection.commitTransaction();
		}
		catch (Exception e)
		{
			DBConnection.rollbackTransaction();
		}

		return result;
	}

	public int updateCustomer(int customerID, String username, String password, boolean active, City city, String email, String phoneNo)
	{
		int result = -3;

		try 
		{
			Customer newCustomer = new Customer();

			newCustomer.setPersonID(customerID);
			newCustomer.setUsername(username);
			newCustomer.setPassword(password);
			newCustomer.setActive(active);
			newCustomer.setCity(city);
			newCustomer.setEmail(email);
			newCustomer.setPhoneNo(phoneNo);

			DBConnection.startTransaction();
			IFCustomer dbCustomer = new DBCustomer();			
			result = dbCustomer.updateCustomer(newCustomer);
			DBConnection.commitTransaction();
		}
		catch (Exception e)
		{
			DBConnection.rollbackTransaction();
		}

		return result;
	}

	public Customer searchCustomerID(int id) throws SQLException
	{
		IFCustomer dbCustomer = new DBCustomer();

		return dbCustomer.searchCustomerID(id);
	}

	public Customer searchCustomerByName(String name) throws SQLException
	{
		IFCustomer dbCustomer = new DBCustomer();

		return dbCustomer.searchCustomerByName(name);
	}

	public ArrayList<Customer> getAllCustomers() throws SQLException
	{
		IFCustomer dbCustomer = new DBCustomer();

		return dbCustomer.getAllCustomers();
	}

	public ArrayList<Customer> searchCustomerName(boolean active, String name) throws SQLException
	{
		IFCustomer dbCustomer = new DBCustomer();

		return dbCustomer.searchCustomerName(active, name);
	}

	public ArrayList<Customer> searchCustomerPhoneNo(boolean active, String phoneno) throws SQLException
	{
		IFCustomer dbCustomer = new DBCustomer();

		return dbCustomer.searchCustomerPhoneNo(active, phoneno);
	}

	public ArrayList<Customer> searchCustomerEmail(boolean active, String email) throws SQLException
	{
		IFCustomer dbCustomer = new DBCustomer();

		return dbCustomer.searchCustomerEmail(active, email);
	}

	public ArrayList<Customer> searchCustomerCity(boolean active, String city) throws SQLException
	{
		IFCustomer dbCustomer = new DBCustomer();

		return dbCustomer.searchCustomerCity(active, city);
	}

	public ArrayList<Customer> searchCustomerCountry(boolean active, String country) throws SQLException
	{
		IFCustomer dbCustomer = new DBCustomer();

		return dbCustomer.searchCustomerCountry(active, country);
	}

	public ArrayList<Customer> searchCustomerEverything(boolean active, String search) throws SQLException
	{
		IFCustomer dbCustomer = new DBCustomer();

		return dbCustomer.searchCustomerEverything(active, search);
	}

	public int activateCustomer(int customerID) throws SQLException
	{
		IFPerson dbPerson = new DBPerson();

		return dbPerson.activatePerson(customerID);		
	}
}